package com.xiaodu.springboot.mybatis.plus;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaodu.springboot.mybatis.plus.entity.UserEntity;
import com.xiaodu.springboot.mybatis.plus.mapper.UserMapper;
import com.xiaodu.springboot.mybatis.plus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import sun.java2d.pipe.SpanIterator;

import java.util.List;
import java.util.UUID;
import java.util.function.LongUnaryOperator;

/**
 * @author: dutianjie
 * @date: 2021/5/10
 * @version: v1.0.0
 * @description: 描述
 **/
@SpringBootApplication
public class MybatisPlusApplication implements ApplicationRunner {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;


    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        UserEntity userEntity = userMapper.selectById("123");
//        System.out.println(userEntity);
//        UserEntity userEntity = new UserEntity();
//        userEntity.setUId("11111");
//        userEntity.setUName("222222");
//        userMapper.insertUser(userEntity);
//        testBaseService();
//        testPage();
//          testWrapper();
//          testLambda();
//        testAR();
//        testId();
//          testTableLogic();
//        testFill();
        testVersion();
    }

    // 测试乐观锁; 只在update情况下生效
    private void testVersion() {

        UserEntity u = new UserEntity();
        u.setUId("1396683484596064257");
        // 获取信息
        UserEntity userEntity = u.selectById();

        // 更新name, 并且带上version字段。
        u.setVersion(userEntity.getVersion());
        u.setUName("Bbb" + UUID.randomUUID());
        // UPDATE t_user SET u_name='Bbb23266788-cb7e-4454-b68d-ef6bc230b09c',
        // gmt_modified='2021-05-24T12:34:51.992', version=5
        // WHERE u_id='1396683484596064257' AND version=4 AND is_del=2
        u.updateById();

    }

    // 测试自动填充
    private void testFill() {
        UserEntity u = new UserEntity();
        u.setUName("fill");
        // INSERT INTO t_user ( u_id, u_name, is_del, gmt_create, gmt_modified ) VALUES ( ?, ?, ?, ?, ? )
        u.insert();
        // UPDATE t_user SET u_name=?, gmt_create=?, gmt_modified=? WHERE u_id=? AND is_del=2
        u.updateById();
    }

    // 测试逻辑删除
    private void testTableLogic() {
        UserEntity u = new UserEntity();
        String id = "logic";
        u.setUId(id);
        u.setUName("testLogic");
        // 逻辑删除对插入是不做限制的， 所以需要我们手动设置
        u.setDel(0);
        // INSERT INTO t_user ( u_id, u_name, is_del ) VALUES ( ?, ?, ? )
        boolean insert = u.insert();
        System.out.println("---------删除--------");
        // UPDATE t_user SET is_del=1 WHERE u_id=? AND is_del=0
        // 删除执行的update 语句， set is_del = 1;
        u.deleteById();
        System.out.println("**********更新*********");
        //UPDATE t_user SET u_name=? WHERE u_id=? AND is_del=0
        // 也会带上条件  is_del =0 （没被删除的才会更新）
        u.updateById();

        System.out.println("===查询=====");
        // SELECT u_id,u_name,is_del AS del FROM t_user WHERE u_id=? AND is_del=0
        // 查询语句中自动添加条件 is_del = 0
        UserEntity userEntity = u.selectById();

    }

    private void testId() {
        UserEntity userEntity  = new UserEntity();
        userEntity.setUName("aaa");
        //INSERT INTO t_user ( u_id, u_name ) VALUES ( ?, ? )
        //Parameters: 1396074749305671681(String), aaa(String); 自动生成了id
        userEntity.insert();
    }

    // 领域模型
    private void testAR() {
        UserEntity userEntity = new UserEntity();
        System.out.println(userEntity.selectAll());
        userEntity.selectOne(new LambdaUpdateWrapper<UserEntity>().eq(UserEntity::getUId,"123"));
        userEntity.selectList(new LambdaQueryWrapper<UserEntity>().like(UserEntity::getUName,"aaa"));
    }

    private void testLambda() {

        // LambdaQueryWrapper
        LambdaQueryWrapper<UserEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(UserEntity::getUId)
                .eq(UserEntity::getUName, "aaa");
        System.out.println(userService.list(lambdaQueryWrapper));

        // LambdaUpdateWrapper
        LambdaUpdateWrapper<UserEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(UserEntity::getUName,"bbb")
                .eq(UserEntity::getUId,"123");
        System.out.println(userService.update(lambdaUpdateWrapper));
    }

    private void testWrapper() {

        // queryWrapper
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        // 使用like，column 为数据库字段
        queryWrapper.like("u_name", "110");
        //SELECT u_id,u_name FROM t_user WHERE (u_name LIKE ?)
        List list = userService.list(queryWrapper);
        System.out.println("===================");

        // 使用  @TableField 设置  condition = SqlCondition.LIKE；进行like 操作
        UserEntity u = new UserEntity();
        u.setUName("aaa");
        QueryWrapper<UserEntity> queryWrapper1 = new QueryWrapper<>(u);
        // SELECT u_id,u_name FROM t_user WHERE u_name LIKE CONCAT('%',?,'%')
        System.out.println(userService.list(queryWrapper1));

        // updateWrapper
        UpdateWrapper<UserEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("u_id", "123")
                .set("u_name", "aaa")
                .eq("u_id", "123"); // 条件
        // UPDATE t_user SET u_id=?,u_name=? WHERE (u_id = ?)
        System.out.println(userService.update(updateWrapper));

        System.out.println("------------------------------------");

        UserEntity userEntity = new UserEntity();
        UpdateWrapper<UserEntity> updateWrapper2 = new UpdateWrapper<>();
        updateWrapper2.eq("u_id","123");
        userEntity.setUName("aaa");
        // 接收2个参数，第一个参数为 set的值， 第二个参数updateWrapper 为条件
        // UPDATE t_user SET u_name=? WHERE (u_id = ?)
        System.out.println(userService.update(userEntity, updateWrapper2));

    }

    private void testPage() {
        Page<UserEntity> page = new Page<>(1,3);
        Page<UserEntity> page1 = userService.page(page);
        System.out.println(page1.getRecords());
    }

    public void testBaseService() {
        System.out.println(userService.getById("123"));
        System.out.println(userService.count());
    }
}
